package czx.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;

import czx.model.Tmenu;
import czx.pageModel.Menu;
import czx.service.MenuServiceI;
import czx.util.HqlExp;

@Service("menuService")
public class MenuServiceImpl extends BaseServiceImpl<Tmenu> implements MenuServiceI {

	private static final Logger logger = Logger.getLogger(MenuServiceImpl.class);

	@Override
	public void test() {
		logger.info("会执行SQL吗？");
	}

	@Override
	public List<Menu> getTree(String id) {
		if (id == null || id.isEmpty()) {
			id = "0";
		}
		HqlExp hqlExp = new HqlExp("from Tmenu t where t.tmenu.id=:id");
		hqlExp.AddParam("id", id);
		List<Tmenu> tList = super.find(hqlExp);

		List<Menu> list = new ArrayList<Menu>();
		if (tList != null && tList.size() > 0) {
			for (Tmenu t : tList) {
				Menu m = new Menu();
				m.setId(t.getMenuid());
				m.setText(t.getMenuname());
				if (t.getTmenu() != null) {
					m.setPid(t.getTmenu().getMenuid());

				}
				if (t.getTmenus() != null && !t.getTmenus().isEmpty()) {
					m.setState("closed");
				} else {
					m.setState("open");
				}
				list.add(m);
			}
		}

		return list;
	}

}
